home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 376 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  140 lines

  1. Newsgroups: comp.lang.c++
  2. Path: peer-news.britain.eu.net!warwick!pcl!news
  3. From: besec@wmin.ac.uk (Nicholas Arthur Llewellyn)
  4. Subject: Using BC4.5 TArrayAsVector of my class in a class!
  5. Message-ID: <DKMHJE.B0B@westminster.ac.uk>
  6. Sender: news@westminster.ac.uk
  7. Nntp-Posting-Host: dip-8.dialup
  8. Organization: University of Westminster
  9. X-Newsreader: Forte Free Agent 1.0.82
  10. Date: Wed, 3 Jan 1996 21:34:59 GMT
  11.  
  12. This is my first use of BC4.5 and I'm trying to use the array class
  13. library to hold the class Card, in the class's Pack and Hand.
  14.  
  15. I get the error:
  16.  
  17. Compiling PROJECT.CPP:
  18.  
  19. <This is the one I'm trying to fix>
  20. Error PROJECT.CPP 71: Cannot find default constructor to initialize
  21. member 'pack' in function Pack::Pack()
  22.  
  23. <These other two I got from messing around trying to fix the above
  24. error>
  25. Error PROJECT.CPP 72: Type qualifier 'pack' must be a struct or class
  26. name in function Pack::Pack()
  27. Error PROJECT.CPP 72: Statement missing ; in function Pack::Pack()
  28.  
  29. The Borland manuals are not much help, all the examples are too
  30. simple.
  31.  
  32. Is there any one out there who can point in the correct direction?
  33.  
  34. Here's the code:
  35.  
  36. // Poker playing program v0.0
  37. // by N A Llewellyn
  38.  
  39. #include <iostream.h>
  40. #include <classlib\arrays.h>
  41.  
  42. // in order of rank from low to high
  43. enum RANK
  44. {two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace};
  45.  
  46. enum SUIT {clubs,harts,diamonds,spades}; // no order of rank
  47.  
  48. enum FACE {up,down};
  49.  
  50. class Card {
  51.  
  52.     private:
  53.         RANK rank;
  54.         SUIT suit;
  55.         FACE face;
  56.  
  57.     friend ostream& operator << (ostream&,const Card&);
  58.  
  59.     public:
  60.         Card() {rank=two;suit=clubs;face=down;}
  61.         Card(const Card & c) {rank=c.rank;suit=c.suit;face=c.face;}
  62.         int operator == (const Card & c) {return rank == c.rank;}
  63.         int operator < (const Card & c) {return rank < c.rank;}
  64.         void SetRank(RANK r) {rank=r;}
  65.         void SetSuit(SUIT s) {suit=s;}
  66.         void SetFace(FACE f) {face=f;}
  67. };
  68.  
  69. typedef TSArrayAsVector <Card> CardArray;
  70. typedef TSArrayAsVectorIterator <Card> CardArrayIterator;
  71.  
  72. class Pack {
  73.  
  74.     private:
  75.         CardArray pack;
  76.         unsigned int NumberOfCards;
  77.  
  78.     public:
  79.         Pack();
  80.         void AddCard(Card c) {pack.Add(c);NumberOfCards++;}
  81.         void FindAndDetach(Card c) {pack.Detach(c);}
  82.         void DetachFrom(int i) {pack.Detach(i);}
  83.         void FlushPack(void) {pack.Flush();NumberOfCards=0;}
  84.         int IsPackEmpty(void) {return pack.IsEmpty();}
  85. };
  86.  
  87. class Hand {
  88.  
  89.     private:
  90.         CardArray hand;
  91.         unsigned int NumberOfCards;
  92.  
  93.     public:
  94.         Hand();
  95.         void AddCard(Card c) {hand.Add(c);NumberOfCards++;}
  96.         void FindAndDetach(Card c) {hand.Detach(c);}
  97.         void DetachFrom(int i) {hand.Detach(i);}
  98.         void FlushPack(void) {hand.Flush();NumberOfCards=0;}
  99.         int IsPackEmpty(void) {return hand.IsEmpty();}
  100.         ~Hand();
  101. };
  102.  
  103. ostream& operator << (ostream& os,const Card& c)
  104.     {return os<<c.rank<<","<<c.suit<<","<<c.face;}
  105.  
  106. // definition of constructor for Pack
  107.  
  108. Pack::Pack()
  109. {
  110.     Card card;
  111.     SUIT suit;
  112.     RANK rank;
  113.  
  114.     NumberOfCards=0;
  115.  
  116.     for(suit=clubs;suit<=spades;suit++)
  117.         for(rank=two;rank<=ace;rank++)
  118.             {
  119.                 card.SetSuit(suit);
  120.                 card.SetRank(rank);
  121.                 card.SetFace(down);
  122.                 AddCard(card);
  123.                 NumberOfCards++;
  124.             }
  125. }
  126.  
  127. void Show(Card& c,void*)
  128.     {cout<<c<<endl;}
  129.  
  130. void UseForEach(CardArray& c)
  131.     {c.ForEach(Show,0);}
  132.  
  133. int main()
  134. {
  135.     Pack pack1;
  136.     UseForEach(pack1.pack);
  137.     return 0;
  138. }
  139.  
  140.